﻿using Common.Msg;
using CToolsWin.DAL.sys;
using Model.sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CToolsWin.BLL.sys
{
    public class BLLDirectoryInfo
    {
        private DALDirectoryInfo dal = new DALDirectoryInfo();
        /// <summary>
        /// 初始化表
        /// </summary>
        public bool InitTable()
        {
            try
            {
                bool bol = dal.InitTable();
                if (bol)
                {
                    bol = new DALFileInfo().InitTable();
                    if (bol == false)
                    {
                        Log.GetLog.Error("初始化表【sys_FileInfo】失败");
                    }
                }
                return bol;
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("初始化表【sys_DirectoryInfo】时发生异常", ex);
                return false;
            }
        }
        public void SaveDirectory(sys_DirectoryInfo m)
        {
            try
            {
                bool bol = dal.IsExists(m);
                if (bol == false)
                    dal.Add(m);
                else
                {
                    sys_DirectoryInfo mNew = dal.GetModel(m);
                    m.ID = mNew.ID;
                }
                if (m.ID > 0)
                {
                    if (m.ListFile != null)
                    {
                        BLLFileInfo bllFile = new BLLFileInfo();
                        foreach (var item in m.ListFile)
                        {
                            item.DirectoryInfoID = m.ID;
                            bllFile.Add(item);
                        }
                    }
                    if (m.ListChilder != null)
                    {
                        foreach (var item in m.ListChilder)
                        {
                            SaveDirectory(item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("保存文件夹信息异常", ex);
            }
        }
    }
}
